﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Utils.DC.Web.UI;
using Entity;
using Utils;
using Utils.DatabaseUtils;

public partial class Pages_AccoutChangeInfo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack) 
        {
            if (Session["loginClient"] != null)
            {
                Customer cus = (Customer)Session["loginClient"];

                if (cus.C_Status)
                {
                    txtFName.Text = cus.F_Name;
                    txtLName.Text = cus.L_Name;
                    txtDate.SelectedDate = cus.Dob;
                    txtAddress.Text = cus.Address;
                    txtPhone.Text = cus.P_No;
                    txtEmail.Text = cus.Email;
                    if (cus.Gender)
                    {
                        radMale.Checked = true;
                    }
                    else
                    {
                        RadFemale.Checked = true;
                    }
                }
                else
                {
                    Response.Redirect("../Pages/AccountLock.aspx");
                }
            }
            else
            {
                Response.Redirect("../Pages/Login.aspx");
            }
        }

    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        CustomerUI customerUI = new CustomerUI();
        DataTable ds = customerUI.GetAll().Tables[0];
        DataRow drEmail = null;
        Customer cus=(Customer)Session["loginClient"];
            try
            {
                drEmail = ds.Select("Email='" + txtEmail.Text + "' and Email <> '"+cus.Email+"'")[0];
                if (drEmail != null)
                {
                    lblEmail.Visible = true;
                }
            }
            catch (Exception ex1)
            {
                    lblEmail.Visible = false;
                    string fname = null;
                    if (txtFName.Text != "")
                    {
                        fname = txtFName.Text;
                    }
                    string lname = null;
                    if (txtLName.Text != "")
                    {
                        lname = txtLName.Text;
                    }

                    DateTime? date = null;
                    if (txtDate.SelectedDate == null)
                    {
                        date = DateTime.Now;
                    }
                    else
                    {
                        date = txtDate.SelectedDate;
                    }

                    bool gender = false;
                    if (radMale.Checked)
                    {
                        gender = true;
                    }
                    else if (RadFemale.Checked)
                    {
                        gender = false;
                    }
                    string phone = null;
                    if (txtPhone.Text != "")
                    {
                        phone = txtPhone.Text.Trim();
                    }
                    string address = null;
                    if (txtAddress.Text != "")
                    {
                        address = txtAddress.Text.Trim();
                    }

                    string email = txtEmail.Text.Trim();

                    int result = customerUI.Update(cus.Cusid,fname,lname,date,gender,phone,address,email);

                    if (result > 0)
                    {
                        CustomerUI cusUI = new CustomerUI();
                        DataSet dsCus = cusUI.GetNewCus(cus.Cusid);
                        if (TableLib.HasData(dsCus))
                        {
                            DataRow row = dsCus.Tables[0].Rows[0];
                            Customer ae = new Customer();
                            ae.Cusid = Convert.ToInt32(row["cusid"].ToString().Trim());
                            ae.Username = row["Username"].ToString().Trim();
                            ae.F_Name = row["F_Name"].ToString();
                            ae.L_Name = row["L_Name"].ToString();
                            ae.Dob = Convert.ToDateTime(row["Dob"].ToString());
                            ae.Gender = Convert.ToBoolean(row["Gender"].ToString());
                            ae.P_No = row["P_No"].ToString();
                            ae.Address = row["Address"].ToString();
                            ae.Email = row["Email"].ToString();
                            ae.C_Status = Convert.ToBoolean(row["C_Status"].ToString()); ;

                            Session["loginClient"] = ae;
                        }
                        Response.Write("<script>alert('Change info successful'); window.location='../Pages/AccountInformation.aspx';</script>");
                    }
                    else
                    {
                        Response.Write("<script>alert('Change info fail');</script>");
                    }
                }

            
        }

    
}
